package org.yx.service.smfr.view_production_order_path_oper;

import java.util.ArrayList;
import java.util.List;

import org.dbist.dml.Query;
import org.yx.models.SmfrOprDef;
import org.yx.models.SmfrOrdOpr;
import org.yx.utils.BaseService;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 查询生产工单的工艺路线及其工序
 * @author zxw
 * @since 2018/04/19
**/
public class SmfrViewProductionOrderPathOperService extends BaseService {
	public SmfrViewProductionOrderPathOperOut viewProductionOrderFlowOper(SmfrViewProductionOrderPathOperIn in)
			throws Exception {
		if (StrUtil.isBlank(in.getMo())) {
			return new SmfrViewProductionOrderPathOperOut();
		}

		Query query = new Query();
		query.addFilter("orgCode", in.getUserOrgCode());
		query.addFilter("mo", in.getMo());

		List<SmfrOrdOpr> ordOprs = selectList(SmfrOrdOpr.class, query);
		List<MoOper> moOpers = new ArrayList<MoOper>();
		
		if (!CollUtil.isEmpty(ordOprs)) {
			query.removeFilter("mo");
			List<SmfrOprDef> opers = selectList(SmfrOprDef.class, query);

			for (SmfrOrdOpr ordOpr : ordOprs) {
				MoOper moOper = new MoOper();
				copyProperties(ordOpr, moOper);

				for (SmfrOprDef oper : opers) {
					if (StrUtil.equals(ordOpr.getOper(), oper.getOper())) {
						moOper.setOperDesc(oper.getOperDesc());

						break;
					}
				}
			}
		}

		SmfrViewProductionOrderPathOperOut out = new SmfrViewProductionOrderPathOperOut();
		out.setOperList(moOpers);

		return out;
	}
}